﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Infragistics.Composite.Wpf.Samples.SearchModule.Tests
{
    [TestClass()]
    public class SearchViewModelTest
    {
        #region Boilerplate Code

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #endregion // Boilerplate Code

        const string INPUT = "  test  Foo C++ 3-2=1 ";
        const string OUTPUT = "http://search.live.com/results.aspx?q=test+Foo+C%2B%2B+3-2%3D1";

        [TestMethod()]
        public void SearchCommandTest()
        {
            SearchViewModel target = new SearchViewModel();

            bool notifiedOfNewUri = false;
            target.PropertyChanged +=
                (sender, e) =>
                {
                    if (e.PropertyName == "SearchResultsUri")
                        notifiedOfNewUri = true;
                };

            Assert.IsFalse(target.SearchCommand.CanExecute(null), "Should not be allowed to search yet");
            target.SearchText = INPUT;
            Assert.IsTrue(target.SearchCommand.CanExecute(null), "Should be allowed to search");    
            Assert.IsTrue(!notifiedOfNewUri, "No notification received of new search result URI");

            target.SearchCommand.Execute(null);
            string actual = target.SearchResultsUri.OriginalString;
            Assert.IsTrue(String.Equals(OUTPUT, actual, StringComparison.InvariantCultureIgnoreCase), "Returned the wrong web address");
        }
    }
}